#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "securec.h"
#include "treeUtils.h"
int max(int x, int  y)
{
    return x - y  > 0 ? x : y;
}
//int maxDepth(struct TreeNode *root) {
//    if (root == NULL) {
//        return 0;
//    }
//    return max(maxDepth(root->left), maxDepth(root->right)) + 1;
//}

struct QueNode {
    struct TreeNode *p;
    struct QueNode *next;
};
void init(struct QueNode **p, struct TreeNode *t)
{
    *p = malloc(sizeof(struct QueNode));
    (*p)->p = t;
    (*p)->next = NULL;
}

int maxDepth(struct TreeNode *root) {
    if (root == NULL) {
        return 0;
    }
    struct QueNode *curNode;
    struct QueNode *nextNode;
    init(&curNode, root);
    nextNode = curNode;
    int ans = 0; int sz = 1;
    while (curNode) {
        int tmp = 0;
        while (sz > 0) {
            if (curNode->p->left) {
                init(&nextNode->next, curNode->p->left);
                nextNode = nextNode->next;
                tmp++;
            }
            if (curNode->p->right) {
                init(&nextNode->next, curNode->p->right);
                nextNode = nextNode->next;
                tmp++;
            }
            curNode = curNode->next;
            sz--;
        }
        sz = tmp;
        ans++;
    }
    return ans;
}

int main()
{
    char str[] = "[3,9,20,null,null,15,7,9]";
    printf("%s\n", "start strtok string.");
    int count = 0;
    char retStrArr[100][10] = {0};
    getNum(str, retStrArr, &count);
    int numArr[9] = {0};
    for (int i = 0; i < count; ++i) {
        printf("%s\n", retStrArr[i]);
        numArr[i] = atoi(retStrArr[i]);
    }
    printf("%s\n", "end strtok string.");

    struct TreeNode *root = malloc(sizeof(struct TreeNode));
    root->val = numArr[0];
    root->left = NULL;
    root->right = NULL;
    bfsBuildTree(root, numArr, count);
    numArr[4] = 0;
    struct TreeNode *root1 = malloc(sizeof(struct TreeNode));
    root1->val = numArr[0];
    root1->left = NULL;
    root1->right = NULL;
    bfsBuildTree(root1, numArr, count);
    int ret = maxDepth(root);
    printf("%d", ret);
}

